#include<iostream>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int n,kk;
struct data
{
	int a,len;
}p[10];
string a[1005],b[1005];
struct fun
{
	int w;
	string x;
}f[1005];
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	for(int i=1;i<=n;i++)
	{
		int pp=0,ppp=0,k=1;
		for(int j=1;j<=5;j++) p[j].a=p[j].len=0;
		for(int j=0;j<b[i].size();j++)
		{
			if(b[i][j]>='0'&&b[i][j]<='9')
			{
				p[k].a=p[k].a*10+(b[i][j]-'0');
				p[k].len++;
			}
			else
			{
				if((pp!=3&&b[i][j]!='.')||(pp==3&&b[i][j]!=':'))
				{
					ppp=1;
					cout<<"ERR"<<endl;
					break;
				}
				if(b[i][j]=='.') pp++;
				k++;
			}
		}
		if(ppp) continue;
		for(int j=1;j<=5;j++)
		{
			if(j==5)
			{
				if(p[j].a>65535)
				{
					ppp=1;
					cout<<"ERR"<<endl;
					break;
				}
			}
			else
			{
				if(p[j].a>255)
				{
					ppp=1;
					cout<<"ERR"<<endl;
					break;
				}
			}
			if(p[j].a==0&&p[j].len!=1)
			{
				ppp=1;
				cout<<"ERR"<<endl;
				break;
			}
		}
		if(ppp) continue;
		if(a[i][0]=='S')
		{
			for(int j=1;j<=kk;j++)
			{
				if(f[j].x==b[i])
				{
					cout<<"FAIL"<<endl;
					ppp=1;
					break;
				}
			}
			if(ppp) continue;
			cout<<"OK"<<endl;
			f[++kk].w=i;
			f[kk].x=b[i];
		}
		if(a[i][0]=='C')
		{
			for(int j=1;j<=kk;j++)
			{
				if(f[j].x==b[i])
				{
					cout<<f[j].w<<endl;
					ppp=1;
					break;
				}
			}
			if(ppp) continue;
			cout<<"FAIL"<<endl;
		}
	}
	return 0;
}
